package com.four.shop.controller;

import com.four.shop.entity.Category;
import com.four.shop.service.CategoryService;
import com.four.shop.util.OssUtils;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;


/**
 * @Author:wsy
 * @Date: 2021/12/9/15:35
 */

@RestController
@RequestMapping("shop/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 三级树
     * @return
     */
    @GetMapping("/list/tree")
    public R treeTwo(){
        List<Category> list=categoryService.tree();
        return R.ok().put("list",list);
    }

    /**
     * 三级树分页
     * @return
     */
    @GetMapping("/treeList")
    public R treeList(@RequestParam Map<String,String> map){

        PageUtils pageUtils = categoryService.treeList(map);
        return R.ok().put("page",pageUtils);
    }

    /**
     * 添加产品分类
     * @param category
     * @return
     */
    @PostMapping("addTree")
    public R addTree(@RequestBody Category category){
        return categoryService.addTree(category);
    }

    /**
     * 添加产品分类的子模块
     * @param category
     * @param id
     * @return
     */
    @PostMapping("addTreeSon/{id}")
    public R addTreeSon(@RequestBody Category category,@PathVariable("id")Integer id){
        categoryService.addTreeSon(id,category);
        return R.ok();
    }

    /**
     * 添加子模块的时候获取到第一级的id
     * 根据id去查询父级id是此id的
     * @param id
     * @return
     */
    @GetMapping("/list/nextLevel/{id}")
    public R nextLevel(@PathVariable("id")Integer id){
        return categoryService.nextLevel(id);
    }


    /**
     * 删除
     * 获取到id，查询此数据有没有子数据
     * @param id
     * @return
     */
    @DeleteMapping("delTree/{id}")
    public int delTree(@PathVariable("id")Integer id){
        return categoryService.delTree(id);
    }

    @PostMapping("uploadCatIcon")
    public R uploadCatIcon(@RequestBody MultipartFile file){
        String c= OssUtils.uploadFile(file);
        return R.ok().put("url",c);
    }
}
